package com.situ.mall.conversion;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

/**
 * 日期转换的Converter String:代表页面传递过来的字符串类型日期 Date:转换后类型
 */
public class DateConverter implements Converter<String, Date> {
	// This method must return a result of type Date
	@Override
	public Date convert(String source) {
		System.out.println("DateConverter.convert()");
		if (null != source) {
			System.out.println(source);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				return simpleDateFormat.parse(source);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	
	public static void main(String[] args) {
		Date date = new Date();
		// Fri Sep 18 20:14:15 CST 2020
		System.out.println(date);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String now = simpleDateFormat.format(date);
		System.out.println(now);// 2020-09-18 08:15:29
	}
}